package ch.sfug.display {
	import flash.net.URLRequest;	
	import flash.display.DisplayObject;	
	import flash.display.StageScaleMode;	
	import flash.events.Event;	
	
	import ch.sfug.net.loader.MediaLoader;	
	
	import flash.display.Sprite;
	
	/**
	 * @author Administrator
	 */
	public class MediaLoaderDisplay extends Sprite {
		
		private var _loader:MediaLoader;
		private var masker:Sprite;
		private var mode:String;

		public function MediaLoaderDisplay( scaleMode:String = StageScaleMode.NO_BORDER ) {
			
			this.scaleMode = scaleMode;
			
			masker = new Sprite();
			masker.graphics.beginFill( 0 );
			masker.graphics.drawRect( 0, 0, 200, 200 );
			masker.graphics.endFill();
			
			_loader = new MediaLoader();
			_loader.addEventListener( Event.COMPLETE, onLoaded);
			
			addChild( _loader.getResult() );
			addChild( masker);
			_loader.getResult().mask = masker;
		}

		private function onLoaded( event:Event ):void {
			align();
		}

		
		public function get loader():MediaLoader {
			return _loader;
		}
		
		public function get scaleMode():String {
			return mode;
		}
		
		public function set scaleMode( smode:String ):void {
			if( smode == StageScaleMode.EXACT_FIT || smode == StageScaleMode.NO_BORDER || smode == StageScaleMode.NO_SCALE || smode == StageScaleMode.SHOW_ALL ) {
				this.mode = smode;
			} else {
				throw new Error( "no supported scaleMode for MediaLoaderDisplay. please use: StageScaleMode.EXACT_FIT, StageScaleMode.NO_BORDER, StageScaleMode.NO_SCALE, StageScaleMode.SHOW_ALL" );
			}
		}

		private function align():void {
			var cont:DisplayObject = _loader.getResult();

			if( cont.width > 0 && cont.height > 0 ) {
				
				var rw:Number = 1;
				var rh:Number = 1;
				var orgH:Number = originalHeight;
				var orgW:Number = originalWidth;
				var w:Number = width;
				var h:Number = height;
		
				cont.x = 0;
				cont.y = 0;
		
				switch ( mode ) {
					case StageScaleMode.EXACT_FIT :
						rw = w/orgW;
						rh = h/orgH;
						break;
					case StageScaleMode.NO_BORDER :
						rw = ( h/orgH > w/orgW ) ? ( h/orgH ) : ( w/orgW );
						rh = ( h/orgH > w/orgW ) ? ( h/orgH ) : ( w/orgW );
						cont.x = Math.round( ( w - ( orgW * rw ) ) / 2 );
						cont.y = Math.round( ( h - ( orgH * rh ) ) / 2 );
						break;
					case StageScaleMode.SHOW_ALL :
						rw = ( h/orgH < w/orgW ) ? ( h/orgH ) : ( w/orgW );
						rh = ( h/orgH < w/orgW ) ? ( h/orgH ) : ( w/orgW );
						cont.x = Math.round( ( w - ( orgW * rw ) ) / 2 );
						cont.y = Math.round( ( h - ( orgH * rh ) ) / 2 );
						break;					
				}
			
				cont.scaleX = rw;
				cont.scaleY = rh;
			}
		}

		public function get originalWidth():Number {
			return _loader.getResult().width / _loader.getResult().scaleX;
		}
		
		public function get originalHeight():Number {
			return _loader.getResult().height / _loader.getResult().scaleY;
		}
		
		public override function set width( w:Number ):void {
			masker.width = w;
			align();
		}
		
		public override function set height( h:Number ):void {
			masker.height = h;
			align();
		}

		public override function get height():Number {
			return masker.height;
		}
		
		public override function get width():Number {
			return masker.width;
		}
		
		public function load( url:String ):void {
			_loader.load( new URLRequest( url ) );
		}
	}
}
